/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.TypeFormulaire;
import Controleur.TypeIncFormControleur;
import Modele.TypeIncident;
import Utils.Constantes;
import java.util.ArrayList;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

/**
 *
 * @author Ben
 */
public class TypeIncidentForm extends javax.swing.JDialog {

    private TypeFormulaire mode;
    private ArrayList<JCheckBox> cb_outils;
    private TypeIncFormControleur controleur;
    private TypeIncident type;

    /**
     * Creates new form TypeIncidentForm
     */
    public TypeIncidentForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode) {
        super(parent, modal);
        initComponents();
        
        //initialisation du controleur
        controleur = new TypeIncFormControleur(this);
        
        this.mode = mode;
        cb_outils = new ArrayList<>();
        for (String s : Constantes.OUTILS) {
            JCheckBox cb = new JCheckBox(s);
            cb_outils.add(cb);
            panel_materiel.add(cb);
        }
        // on active les controles en fonction du mode
        appliquerMode();
    }

    /**
     * Creates new form TypeIncidentForm
     */
    public TypeIncidentForm(java.awt.Frame parent, boolean modal, TypeFormulaire mode, TypeIncident type) {
        this(parent, modal, mode);
        this.type = type;
        if (mode != TypeFormulaire.CREATION) {
            tf_nom.setText(type.getTypeincNom());
            ta_description.setText(type.getMDescription());
            ta_proc.setText(type.getMProcedure());
            checkOutils(type.getTypeincOutils());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCheckBox1 = new javax.swing.JCheckBox();
        label_titre = new javax.swing.JLabel();
        label_nom = new javax.swing.JLabel();
        tf_nom = new javax.swing.JTextField();
        label_description = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_description = new javax.swing.JTextArea();
        label_proc = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        ta_proc = new javax.swing.JTextArea();
        label_materiel = new javax.swing.JLabel();
        panel_materiel = new javax.swing.JPanel();
        panel_boutons = new javax.swing.JPanel();
        btn_annuler = new javax.swing.JButton();
        btn_enregistrer = new javax.swing.JButton();
        btn_activ_modif = new javax.swing.JButton();
        btn_update = new javax.swing.JButton();

        jCheckBox1.setText("jCheckBox1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        label_titre.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        label_titre.setText("Type Incident");

        label_nom.setText("Nom");

        tf_nom.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_nomActionPerformed(evt);
            }
        });

        label_description.setText("Descritpion");

        ta_description.setColumns(20);
        ta_description.setRows(5);
        jScrollPane1.setViewportView(ta_description);

        label_proc.setText("Procédure");

        ta_proc.setColumns(20);
        ta_proc.setRows(5);
        jScrollPane2.setViewportView(ta_proc);

        label_materiel.setText("Materiel");

        btn_annuler.setText("Annuler");
        btn_annuler.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_annulerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_annuler);

        btn_enregistrer.setText("Enregistrer");
        btn_enregistrer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_enregistrerActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_enregistrer);

        btn_activ_modif.setText("Modifier");
        btn_activ_modif.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_activ_modifActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_activ_modif);

        btn_update.setText("Enregistrer");
        btn_update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_updateActionPerformed(evt);
            }
        });
        panel_boutons.add(btn_update);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(label_nom)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(133, 133, 133)
                                .addComponent(label_titre, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(37, 37, 37)
                                .addComponent(tf_nom, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(panel_boutons, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(label_materiel, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(label_proc, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(label_description, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(panel_materiel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 10, Short.MAX_VALUE)))
                        .addGap(43, 43, 43)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label_titre, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(label_nom, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tf_nom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(label_description)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(label_proc)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(label_materiel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(panel_materiel, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panel_boutons, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_nomActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_nomActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_nomActionPerformed

    private void btn_enregistrerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_enregistrerActionPerformed
        
        if(tf_nom.getText().equals("") ||
                ta_description.getText().equals("") ||
                ta_proc.getText().equals("")){
            JOptionPane.showMessageDialog(this,
                        "Champ manquant", "Erreur", JOptionPane.WARNING_MESSAGE);
        }else{
            btn_enregistrer.setEnabled(false);
            //création de la chaine d'outils
            StringBuilder builder = new StringBuilder();
            for(int i = 0 ; i < cb_outils.size() ; i++){
                if(cb_outils.get(i).isSelected()){
                    builder.append(cb_outils.get(i).getText());
                    if(i != cb_outils.size()-1){
                        builder.append(Constantes.SPLIT);
                    }
                }
            }
            type.setTypeincNom(tf_nom.getText());
            type.setMDescription(ta_description.getText());
            type.setMProcedure(ta_proc.getText());
            type.setTypeincOutils(builder.toString());
            controleur.enregistrer(type);
        }
        
    }//GEN-LAST:event_btn_enregistrerActionPerformed

    private void btn_activ_modifActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_activ_modifActionPerformed
        mode = TypeFormulaire.EDITION;
        appliquerMode();
    }//GEN-LAST:event_btn_activ_modifActionPerformed

    private void btn_annulerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_annulerActionPerformed
        this.dispose();
    }//GEN-LAST:event_btn_annulerActionPerformed

    private void btn_updateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_updateActionPerformed
        
        if(tf_nom.getText().equals("") ||
                ta_description.getText().equals("") ||
                ta_proc.getText().equals("")){
            JOptionPane.showMessageDialog(this,
                        "Champ manquant", "Erreur", JOptionPane.WARNING_MESSAGE);
        }else{
            btn_update.setEnabled(false);
            //création de la chaine d'outils
            StringBuilder builder = new StringBuilder();
            for(int i = 0 ; i < cb_outils.size() ; i++){
                if(cb_outils.get(i).isSelected()){
                    builder.append(cb_outils.get(i).getText());
                    if(i != cb_outils.size()-1){
                        builder.append(Constantes.SPLIT);
                    }
                }
            }
            type.setTypeincNom(tf_nom.getText());
            type.setMDescription(ta_description.getText());
            type.setMProcedure(ta_proc.getText());
            type.setTypeincOutils(builder.toString());
            controleur.modifier(type);
        }
    }//GEN-LAST:event_btn_updateActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                String description = "Un ou plusieurs pneu(s) du vélo sont crevés.";
                String proc = "1 - Retirer le pneu crevé.\n2 - Si la crevaison est faible, réparer avec une rustine. Sinon changer la chambre à air.\n3 - Remonter le pneu";
                String outils = "Rustine:Pompe:Tournevis plat";
                TypeIncident typeI = new TypeIncident("Pneu crevé", description, proc, outils);
                typeI.setMId(1);
                TypeIncidentForm dialog = new TypeIncidentForm(new javax.swing.JFrame(), true, TypeFormulaire.AFFICHAGE, typeI);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private void appliquerMode() {
        switch (mode) {
            case AFFICHAGE:
                activerChamps(false);
                //activation des boutons
                btn_activ_modif.setVisible(true);
                btn_annuler.setVisible(false);
                btn_enregistrer.setVisible(false);
                btn_update.setVisible(false);
                break;
            case CREATION:
                activerChamps(true);
                //activation des boutons
                btn_activ_modif.setVisible(false);
                btn_annuler.setVisible(true);
                btn_enregistrer.setVisible(true);
                btn_update.setVisible(false);
                break;
            case EDITION:
                activerChamps(true);
                //activation des boutons
                btn_activ_modif.setVisible(false);
                btn_annuler.setVisible(true);
                btn_enregistrer.setVisible(false);
                btn_update.setVisible(true);
                break;
            default:
                break;
        }
    }

    private void activerChamps(boolean bool) {
        tf_nom.setEditable(bool);
        ta_description.setEditable(bool);
        ta_proc.setEditable(bool);
        for (JCheckBox cb : cb_outils) {
            cb.setEnabled(bool);
        }
    }

    private void checkOutils(String s) {
        String outils[] = s.split(Constantes.SPLIT);
        for (String outil : outils) {
            for (JCheckBox cb : cb_outils) {
                if (outil.equals(cb.getText())) {
                    cb.setSelected(true);
                }
            }
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_activ_modif;
    private javax.swing.JButton btn_annuler;
    private javax.swing.JButton btn_enregistrer;
    private javax.swing.JButton btn_update;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel label_description;
    private javax.swing.JLabel label_materiel;
    private javax.swing.JLabel label_nom;
    private javax.swing.JLabel label_proc;
    private javax.swing.JLabel label_titre;
    private javax.swing.JPanel panel_boutons;
    private javax.swing.JPanel panel_materiel;
    private javax.swing.JTextArea ta_description;
    private javax.swing.JTextArea ta_proc;
    private javax.swing.JTextField tf_nom;
    // End of variables declaration//GEN-END:variables
}
